package com.mohan.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mohan.common.result.Result;
import com.mohan.common.utils.MD5;
import com.mohan.model.hosp.HospitalSet;
import com.mohan.service.HospitalSetService;
import com.mohan.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@Api(tags = "医院设置管理")
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin
public class HospitalSerController {
  @Autowired
  private HospitalSetService hospitalSetService;

  @GetMapping("/findAll")
  @ApiOperation(value = "获取所有医院设置")
  public Result findAllHospitalSer() {
    return Result.ok(hospitalSetService.list(null));
  }

  @DeleteMapping("/deleteById/{id}")
  @ApiOperation(value = "根据id删除")
  public Result removeHospSet(@PathVariable Long id) {
    return hospitalSetService.removeById(id) ? Result.ok() : Result.fail();
  }

  @PostMapping("/findPageHospSet/{current}/{limit}")
  @ApiOperation(value = "分页查询医院设置")
  public Result findPageHospSet(@PathVariable long current,
                                @PathVariable long limit,
                                @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
    //1. 创建page对象，传递当前页，每页记录数
    Page<HospitalSet> page = new Page<>(current, limit);
    //2. 构造条件
    QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
    if (!StringUtils.isEmpty(hospitalSetQueryVo.getHosname()))
      wrapper.like("hosname", hospitalSetQueryVo.getHosname());
    if (!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode()))
      wrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
    //3. 调用方法实现分页
    Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, wrapper);
    return Result.ok(hospitalSetPage);
  }

  @PostMapping("/saveHostSet")
  @ApiOperation(value = "保存医院设置")
  public Result saveHostSet(@RequestBody HospitalSet hospitalSet) {
    hospitalSet.setStatus(1);
    // 签名密钥
    Random random = new Random();
    hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
    return hospitalSetService.save(hospitalSet) ? Result.ok() : Result.fail();
  }

  @GetMapping("/getHospSet/{id}")
  @ApiOperation(value = "根据id查询医院设置")
  public Result getHospSet(@PathVariable long id) {
    return Result.ok(hospitalSetService.getById(id));
  }

  @PostMapping("/updateHospitalSet")
  @ApiOperation(value = "修改医院设置")
  public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
    return hospitalSetService.updateById(hospitalSet) ? Result.ok() : Result.fail();
  }

  @DeleteMapping("/batchRemove")
  @ApiOperation(value = "批量删除")
  public Result batchRemove(@RequestBody List<Long> ids) {
    return hospitalSetService.removeByIds(ids) ? Result.ok() : Result.fail();
  }

  @PutMapping("/lockHospitalSet/{id}/{status}")
  @ApiOperation("医院设置锁定和解锁")
  public Result lockHospitalSet(@PathVariable long id, @PathVariable int status) {
    // 根据id查询医院设置信息
    return hospitalSetService.updateById(new HospitalSet(id, status)) ? Result.ok() : Result.fail();
  }

  @PutMapping("/sendKey/{id}")
  @ApiOperation("发送签名密钥")
  public Result sendKey(@PathVariable Long id) {
    HospitalSet hospitalSet = hospitalSetService.getById(id);
    String signKey = hospitalSet.getSignKey();
    String hoscode = hospitalSet.getHoscode();
    // TODO: 2022/2/7 发送短信
    return Result.ok();
  }
}
